import { ref, computed } from 'vue'
import { defineStore } from 'pinia'
import type { ContentItem } from '@/types'

export const useContentStore = defineStore('content', () => {
  // State
  const contents = ref<ContentItem[]>([])
  const loading = ref(false)
  const error = ref<string | null>(null)

  // Getters
  const getContentByType = computed(() => {
    return (type: string) => contents.value.filter(content => content.content_type === type)
  })

  const getContentByCategory = computed(() => {
    return (category: string) => contents.value.filter(content => content.category === category)
  })

  const getContentByPlatform = computed(() => {
    return (platform: string) => contents.value.filter(content => content.platform === platform)
  })

  const totalContents = computed(() => contents.value.length)

  // Actions
  const fetchContents = async() => {
    loading.value = true
    error.value = null
    try {
      // TODO: 实现获取内容列表的逻辑
      console.log('Fetching contents...')
    } catch (err) {
      error.value = err instanceof Error ? err.message : 'Unknown error'
    } finally {
      loading.value = false
    }
  }

  const createContent = async(content: Omit<ContentItem, 'id' | 'created_at' | 'updated_at'>) => {
    // TODO: 实现创建内容的逻辑
    console.log('Creating content:', content)
  }

  const updateContent = async(id: string, updates: Partial<ContentItem>) => {
    // TODO: 实现更新内容的逻辑
    console.log('Updating content:', id, updates)
  }

  const deleteContent = async(id: string) => {
    // TODO: 实现删除内容的逻辑
    console.log('Deleting content:', id)
  }

  const copyContent = async(id: string) => {
    // TODO: 实现复制内容的逻辑
    console.log('Copying content:', id)
  }

  return {
    // State
    contents,
    loading,
    error,

    // Getters
    getContentByType,
    getContentByCategory,
    getContentByPlatform,
    totalContents,

    // Actions
    fetchContents,
    createContent,
    updateContent,
    deleteContent,
    copyContent
  }
})